home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: problem passing a string
- Date: 9 Mar 1996 15:48:37 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4hs98l$l8i@sparcserver.lrz-muenchen.de>
- References: <4hrtsp$4mf@mulgave.octacon.co.uk> <4hrv48$huo@news.acns.nwu.edu>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- muzaffar@casbah.acns.nwu.edu (Usman Muzaffar) writes:
-
- >In article <4hrtsp$4mf@mulgave.octacon.co.uk>,
- >Nik Palmer <Nik.Palmer@onyx.octacon.co.uk> wrote:
- >>Hi, Im using a textoutput routine in the fastgraph package of the
- >>form below
- >> void fg_print(char *string,int n);
- >>The string that I want to pass is initialised as below
- >>
- >>char string[6]
- >>double variable= 1.2345
- >>sprintf(string1,"%5f",variable)
-
- Is there any relation between "string" and "string1"?
-
- >>
- >>fg_print(string1,5)
- >>
- >>problem is it's outputting garbage,
-
- Do you observe the same behaviour when you call, e.g., puts()?
-
- >> I know that string1 is only a
- >>pointer to the memory that holds the string, and that if I want to
- >>access the string I need string1[num]. So I think it's just
- >>outputting 5 bytes of memory from the memory location string1.
-
- Since we do _not_ know what "string1" _is_ in your program, this
- is quite hard to answer. Since you seem to talk about an array
- of char, I will assume that "string1" is declared as "char string1[6]".
-
- In this case, string1 is an array of 6 characters, and yes, it
- decays to a pointer to the first element when used in an expression
- context. This is _not_ a problem in general.
-
- {
- char string[6];
- double variable = 1.2345;
-
- sprintf(string, "%5.3f", variable);
- puts(string);
- }
-
- This should work quite well, without declaring string as static.
-
- Why did I change Nik's format string? Because the optional "width"
- specifier denotes the _mimimum_ width used by the output routine,
- and because the output will be padded to have _at least_ the
- desired width. This does _not_ specify a maximum width for the
- output produced. Therefore, "%5f" might easily use _more_ than 5
- characters and "overrun" the available memory.
-
- >>
- >>How do I pass this routine the string??
- >>Thanks for your help.
- >>Nik
-
- >I'll bet anything that "char string[6] " is declared locally
- >inside some function. The problem is that the pointer you pass
- >refers to memory that fg_print can't access. It's a valid string
- >pointer, alright, but not in a valid location.
-
- >Remember, only globals & data on the heap (stuff made by malloc and calloc, for
- >example) is truly accessible all the time. Anything declared inside
- >a function (including main()) is NOT valid outside that function.
-
- This is _not_ true. Local variables "disappear" when you leave a
- function, but not when you call another function from a function.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
- | ua302aa@sunmail.lrz-muenchen.de
-